<?php
	// The hook function of the parser is configured
	$wgHooks['ParserFirstCallInit'][] = 'efTransclusionIDParserInit';

	// The magic word hook of the parser is configured
	$wgHooks['LanguageGetMagic'][] = 'efTransclusionIDParserFunction_Magic';

	// A function is called before display the final page (this is used for scripting)
	$wgHooks['BeforePageDisplay'][] = 'efShowTransclusionMenu';

	function efTransclusionIDParserInit( &$parser ) {
		// tag <transclusionid-xxxxx> (generated xxxxx) will be handled by tagFunctionTrinclude
		createHooks( $parser );

		// the magic word 'transcludeid' will be associated with a parser function
		$parser -> setFunctionHook( 'transcludeid' , 'parserFunctionTrinclude' );
		return true;
	}

	function efTransclusionIDParserFunction_Magic( &$magicWords, $langCode ) {
		// The magic word is added. 0 is non-case sensitive and 1 is case sensitive
		// Any other parameter is a synonym of the magic word
		$magicWords['transcludeid'] = array( 0 , 'transcludeid' );

		return true;
	}

	function efShowTransclusionMenu(){
		global $wgOut;
		global $wgScriptPath;

		$scripts2include = $wgScriptPath . '/extensions/MediaWikiExtension/include';

		$wgOut -> addScript( '<script src="' . $scripts2include . '/lib/js/jquery-1.7.min.js" type="text/javascript"></script>' );
		$wgOut -> addScript( '<script src="' . $scripts2include . '/lib/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>' );
		$wgOut -> addScript( '<script src="' . $scripts2include . '/lib/js/jquery.ajaxq-0.0.1.js" type="text/javascript"></script>' );
		$wgOut -> addScript( '<script src="' . $scripts2include . '/lib/js/processing-1.3.6.min.js" type="text/javascript"></script>' );

		$wgOut -> addScript ( '<script src="' . $scripts2include . '/js/MediaWikiExtension.js" type="text/javascript"></script>' );

		$url = $wgScriptPath . '/extensions/MediaWikiExtension/include/';

		$divButtons = '<div id="transclusionmenu"><h5>Transclusion</h5>';

		if( $_SESSION['trTSourceArticle'] != '[source]'){
			$divButtons = $divButtons . '<input type="checkbox" id="source_article" checked="checked"/><span id="source_article_name"><b>' . $_SESSION['trTSourceArticle'] /*$trTSourceArticle*/ . '</b> [SET]</span><br/>';
		}
		else{
			$divButtons = $divButtons . '<input type="checkbox" id="source_article"/><span id="source_article_name">[source] [UNSET]</span><br/>';
		}

		if( $_SESSION['trTTargetArticle'] != '[target]'){
			$divButtons = $divButtons . '<input type="checkbox" id="target_article" checked="checked"/><span id="target_article_name"><b>' . $_SESSION['trTTargetArticle'] . '</b> [SET]</span><br/>';
		}
		else{
			$divButtons = $divButtons . '<input type="checkbox" id="target_article"/><span id="target_article_name">[target] [UNSET]</span><br/>';
		}

		$divButtons = $divButtons . '<input id="urlDocumento" type="hidden" value="' . $url . '"/>';
		
		$divButtons = $divButtons . '<h5>W.U.V.</h5><input id="openWUV" type="button" value="Open WUV"/></div>';
		$wgOut -> addHTML($divButtons);

		return true;
	}

	/**
	 * Method that creates a hook with every function on the database.
	 */
	function createHooks( &$parser ){
		$transclusiones = TransclusionDAO::singleton() -> getTransclusionMatrix();

		$i=0;
		$nroElementos = count($transclusiones);

		while($i < $nroElementos){
			$parser -> setHook( 'transclusionid-' . $transclusiones[$i]["trid"] , 'tagFunctionTrinclude' );

			$i++;
		}
	}



?>